package com.livegoods.service;

import com.livegoods.commons.pojo.LivegoodsResult;
import com.livegoods.pojo.House;
import lombok.Data;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DetailServiceImpl implements  DetailService {


    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private RedisTemplate redisTemplate;

    @Value("${livegoods.fastdfs.nginx}")
    private String fastdfsNginx;


    @Cacheable(cacheNames = "com.livegoods",key = "'details:'+#id")
    public House findById(String id) {
        System.out.println("id = " + id);
        try {
            House house = mongoTemplate.findById(id, House.class);
            List<String> imgs = house.getImgs();

            for (int i = 0; i < imgs.size(); i++) {
                imgs.set(i,fastdfsNginx+imgs.get(i));
            }
            house.setImgs(imgs);

            return house;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public Map<String, Object> buytime(String id) {
        try {
            House house = mongoTemplate.findById(id, House.class);

            Date buytime = house.getBuytime();


            Map<String,Object> resultMap = new HashMap<>();

            resultMap.put("status",200);
            resultMap.put("time",buytime.getTime());

            return resultMap;
        } catch (Exception e) {
            e.printStackTrace();

            Map<String,Object> resultMap = new HashMap<>();

            resultMap.put("status",500);
            resultMap.put("msg","获取预定时间失败，请联系管理员");
            return resultMap;
        }
    }
}
